package com.tz.tzgw.pc.web;


import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;


import com.tz.tzgw.pc.web.util.HttpReqUtil;
import com.tz.tzgw.pc.web.util.ShareUtil;

@Controller
public class WechatController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    //appid  公众号的appId
    public static final String APP_ID = "wxd9f8add4d5829a97";
    //appsecret   公众号的appSecret
    public static final String SECRET = "0ca30c8587fe1014b7627285cdfc1656";

    /**
     * 获取微信签名
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("get_wechat_config")
    @ResponseBody
    public Map<String, String> get_wechat_config(@RequestParam("timestamp") String timestamp,@RequestParam("url") String url,@RequestParam("nonce_str") String nonce_str, HttpServletRequest request, HttpServletResponse response) {

        Map<String, String> ret = new HashMap<>();

        //获取前台传来的三个参数
//        String timestamp = request.getParameter("timestamp");
//        String nonce_str = request.getParameter("nonce_str");
//        String url = request.getParameter("url");
        logger.info("url"+url+"==============="+nonce_str+"============"+timestamp);

        //从缓存中读取token信息，如果没有则获取一个新的token，通过token获取ticket信息
        String access_token = (String)request.getSession().getAttribute("access_token");
        String token = "";

        if(access_token == null) {

            /** 获取AccessToKen*/
            String getAccessToKen = "https://api.weixin.qq.com/cgi-bin/token?"
                    + "grant_type=client_credential&appid="+APP_ID+"&secret="+ SECRET;
            JSONObject jsonObject = HttpReqUtil.HttpsDefaultExecute("GET", getAccessToKen, null, null, null, JSONObject.class);
            token = jsonObject.get("access_token").toString();
            /**获取jsapi_ticket*/
            String getTicket = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+jsonObject.get("access_token").toString()+"&type=jsapi";
            JSONObject rest = HttpReqUtil.HttpsDefaultExecute("GET", getTicket, null, null, null, JSONObject.class);

            /**将信息保存入缓存中*/
            request.getSession().setAttribute("token", jsonObject.get("access_token").toString());
            request.getSession().setAttribute("ticket", rest.get("ticket").toString());
        }
        /**生成签名*/
        String ticket = (String)request.getSession().getAttribute("ticket"); //微信返回的ticket
        String signature = ShareUtil.getSignature(ticket,url,nonce_str,timestamp); //获取签名
        ret.put("appId", APP_ID);
        ret.put("nonceStr", nonce_str);
        ret.put("timestamp", timestamp);
        ret.put("signature", signature);
//        ret.put("token", token);
//        ret.put("ticket", ticket);
        return ret;
    }

}
